package com.itheima.service.system.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.DeptDao;
import com.itheima.domain.system.Dept;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.DeptService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

/**
 * @program: Web综合案例
 * @author: DingYaDong
 * @create: 2021-06-23 17:18
 */
public class DeptServiceImpl implements DeptService {


    @Override
    public PageInfo findAll(int page,int size) {
        PageInfo PageInfo = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);
            PageHelper.startPage(page,size);
            List<Dept> all = deptDao.findAll();
            PageInfo = new PageInfo(all);
            return PageInfo;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public List<Dept> findAll() {

        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);

            List<Dept> all = deptDao.findAll();

            return all;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public Dept findById(String id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);
            Dept byId = deptDao.findById(id);
            return byId;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }


    @Override
    public int delete(String id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);
            int delete = deptDao.delete(id);
            TransactionUtil.commit(sqlSession);
            return delete;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }


    @Override
    public int save(Dept dept) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            String id = UUID.randomUUID().toString();
            dept.setId(id);
            DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);
            int save = deptDao.save(dept);
            TransactionUtil.commit(sqlSession);
            return save;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public int update(Dept dept) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);
            int update = deptDao.update(dept);
            TransactionUtil.commit(sqlSession);
            return update;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }
}
